package com.chenl.provider.DEMOS.Bcrypt加密;

import cn.hutool.crypto.SecureUtil;

import com.alibaba.fastjson.JSONObject;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;
import java.util.function.Function;

@RestController
@RequestMapping("/bcript")
public class BcryptController {
    @PostMapping("/test")
    public String bcryptTest(){
        String salt = BCrypt.gensalt();
        long l = System.currentTimeMillis();
        String hashpw = BCrypt.hashpw("99360..00", salt);//加密
        long l2 = System.currentTimeMillis();
        System.out.println(salt);
        System.out.println(hashpw);
        System.out.println(l2-l);

        return "ok";
    }
    @PostMapping("/test2")
    public Boolean bcryptTest2(@RequestBody JSONObject jsonObject){
        String password = jsonObject.getString("password");
        String hash = jsonObject.getString("hash");
        long l = System.currentTimeMillis();
        boolean checkpw =false;
        for (int i = 0; i < 1; i++) {
            checkpw=BCrypt.checkpw(password, hash);//解密验证
        }
        long l2 = System.currentTimeMillis();
        System.out.println(l2-l);
        return checkpw;
    }
    @PostMapping("/test3")
    public String bcryptTest3(){
        long l = System.currentTimeMillis();
        String s = SecureUtil.md5(String.valueOf(UUID.randomUUID())+"99360..00$2a$10$Jo9uUc7DKmPIjo2VRkOLTe");
        long l2 = System.currentTimeMillis();
        System.out.println(s);
        System.out.println(l2-l);
        return "ok";
    }
}
